package com.changgou.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import com.changgou.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author tang.qingbo
 * @Date 2020/9/5 0:20
 * @Version 1.0
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SpuFeign spuFeign;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加购物车
     * 参数1:购买数量
     * 参数2:购买id==skuId
     * 参数3:购买用户
     *
     * @param num
     * @param id
     * @param username
     */
    @Override
    public void add(Integer num, String id, String username) {
        //删除商品的购物车数据
        if (num <= 0) {
            redisTemplate.boundHashOps("Cart_" + username).delete(id);
            return;
        }
        //获取sku信息
        Result<Sku> skuResult = skuFeign.findById(id);
        //isFlag代表执行是否成功
        if (skuResult != null && skuResult.isFlag()) {
            //获取sku
            Sku sku = skuResult.getData();
            //通过sku中的spuId查询spu
            Result<Spu> spuResult = spuFeign.findById(sku.getSpuId());

            //转换成orderItem
            OrderItem orderItem = skuToOrderItem(sku, spuResult.getData(), num);

            /**
             * 购物车信息放入Redis
             */
            redisTemplate.boundHashOps("Cart_" + username).put(id, orderItem);
        }
    }

    /**
     * 查询用户购物车数据
     *
     * @param username
     */
    @Override
    public List list(String username) {
        return redisTemplate.boundHashOps("Cart_" + username).values();
    }

    /**
     * 根据选中的商品id,查询对应的商品信息
     *
     * @param ids
     */
    @Override
    public List<OrderItem> findChooseList(String[] ids,String username) {
        //获取当前用户的所有购物车数据
        List<OrderItem> values = redisTemplate.boundHashOps("Cart_" + username).values();
        //创建集合,保存选中的数据
        List<OrderItem> orderItemList = new ArrayList<>();
        //循环遍历==获取当前选中商品信息
        if (values != null) {
            for (OrderItem orderItem : values) {
                for (String id : ids) {
                    if (orderItem.getSkuId().equals(id)) {
                        orderItemList.add(orderItem);
                        //匹配成功,结束当次循环,进行下一次循环
                        break;
                    }
                }
            }
        }
        //返回结果
        return orderItemList;
    }

    /**
     * sku转成orderItem
     */
    private OrderItem skuToOrderItem(Sku sku, Spu spu, Integer num) {
        OrderItem orderItem = new OrderItem();
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());
        orderItem.setSpuId(sku.getSpuId());
        orderItem.setSkuId(sku.getId());
        orderItem.setName(sku.getName());
        orderItem.setPrice(sku.getPrice());
        orderItem.setNum(num);
        orderItem.setMoney(num * sku.getPrice());
        orderItem.setImage(sku.getImage());
        orderItem.setWeight(sku.getWeight());
        return orderItem;
    }
}
